{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import sys\n",
    "if \"../\" not in sys.path:\n",
    "  sys.path.append(\"../\") \n",
    "from lib.envs.blackjack import BlackjackEnv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "env = BlackjackEnv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Player Score: 19 (Usable Ace: False), Dealer Score: 5\n",
      "Taking action: Hit\n",
      "Player Score: 27 (Usable Ace: False), Dealer Score: 5\n",
      "Game end. Reward: -1.0\n",
      "\n",
      "Player Score: 21 (Usable Ace: True), Dealer Score: 10\n",
      "Taking action: Stick\n",
      "Player Score: 21 (Usable Ace: True), Dealer Score: 10\n",
      "Game end. Reward: 0.0\n",
      "\n",
      "Player Score: 21 (Usable Ace: True), Dealer Score: 10\n",
      "Taking action: Stick\n",
      "Player Score: 21 (Usable Ace: True), Dealer Score: 10\n",
      "Game end. Reward: 1.0\n",
      "\n",
      "Player Score: 14 (Usable Ace: True), Dealer Score: 10\n",
      "Taking action: Hit\n",
      "Player Score: 19 (Usable Ace: True), Dealer Score: 10\n",
      "Taking action: Hit\n",
      "Player Score: 15 (Usable Ace: False), Dealer Score: 10\n",
      "Taking action: Hit\n",
      "Player Score: 20 (Usable Ace: False), Dealer Score: 10\n",
      "Taking action: Stick\n",
      "Player Score: 20 (Usable Ace: False), Dealer Score: 10\n",
      "Game end. Reward: 1.0\n",
      "\n",
      "Player Score: 20 (Usable Ace: False), Dealer Score: 10\n",
      "Taking action: Stick\n",
      "Player Score: 20 (Usable Ace: False), Dealer Score: 10\n",
      "Game end. Reward: 1.0\n",
      "\n",
      "Player Score: 18 (Usable Ace: False), Dealer Score: 6\n",
      "Taking action: Hit\n",
      "Player Score: 27 (Usable Ace: False), Dealer Score: 6\n",
      "Game end. Reward: -1.0\n",
      "\n",
      "Player Score: 16 (Usable Ace: False), Dealer Score: 3\n",
      "Taking action: Hit\n",
      "Player Score: 18 (Usable Ace: False), Dealer Score: 3\n",
      "Taking action: Hit\n",
      "Player Score: 23 (Usable Ace: False), Dealer Score: 3\n",
      "Game end. Reward: -1.0\n",
      "\n",
      "Player Score: 19 (Usable Ace: False), Dealer Score: 10\n",
      "Taking action: Hit\n",
      "Player Score: 23 (Usable Ace: False), Dealer Score: 10\n",
      "Game end. Reward: -1.0\n",
      "\n",
      "Player Score: 19 (Usable Ace: False), Dealer Score: 4\n",
      "Taking action: Hit\n",
      "Player Score: 21 (Usable Ace: False), Dealer Score: 4\n",
      "Taking action: Stick\n",
      "Player Score: 21 (Usable Ace: False), Dealer Score: 4\n",
      "Game end. Reward: 1.0\n",
      "\n",
      "Player Score: 21 (Usable Ace: True), Dealer Score: 4\n",
      "Taking action: Stick\n",
      "Player Score: 21 (Usable Ace: True), Dealer Score: 4\n",
      "Game end. Reward: 1.0\n",
      "\n",
      "Player Score: 16 (Usable Ace: True), Dealer Score: 10\n",
      "Taking action: Hit\n",
      "Player Score: 16 (Usable Ace: False), Dealer Score: 10\n",
      "Taking action: Hit\n",
      "Player Score: 26 (Usable Ace: False), Dealer Score: 10\n",
      "Game end. Reward: -1.0\n",
      "\n",
      "Player Score: 14 (Usable Ace: False), Dealer Score: 10\n",
      "Taking action: Hit\n",
      "Player Score: 23 (Usable Ace: False), Dealer Score: 10\n",
      "Game end. Reward: -1.0\n",
      "\n",
      "Player Score: 12 (Usable Ace: False), Dealer Score: 10\n",
      "Taking action: Hit\n",
      "Player Score: 15 (Usable Ace: False), Dealer Score: 10\n",
      "Taking action: Hit\n",
      "Player Score: 16 (Usable Ace: False), Dealer Score: 10\n",
      "Taking action: Hit\n",
      "Player Score: 26 (Usable Ace: False), Dealer Score: 10\n",
      "Game end. Reward: -1.0\n",
      "\n",
      "Player Score: 16 (Usable Ace: True), Dealer Score: 8\n",
      "Taking action: Hit\n",
      "Player Score: 18 (Usable Ace: True), Dealer Score: 8\n",
      "Taking action: Hit\n",
      "Player Score: 18 (Usable Ace: False), Dealer Score: 8\n",
      "Taking action: Hit\n",
      "Player Score: 20 (Usable Ace: False), Dealer Score: 8\n",
      "Taking action: Stick\n",
      "Player Score: 20 (Usable Ace: False), Dealer Score: 8\n",
      "Game end. Reward: 1.0\n",
      "\n",
      "Player Score: 20 (Usable Ace: False), Dealer Score: 10\n",
      "Taking action: Stick\n",
      "Player Score: 20 (Usable Ace: False), Dealer Score: 10\n",
      "Game end. Reward: -1.0\n",
      "\n",
      "Player Score: 15 (Usable Ace: False), Dealer Score: 10\n",
      "Taking action: Hit\n",
      "Player Score: 16 (Usable Ace: False), Dealer Score: 10\n",
      "Taking action: Hit\n",
      "Player Score: 23 (Usable Ace: False), Dealer Score: 10\n",
      "Game end. Reward: -1.0\n",
      "\n",
      "Player Score: 12 (Usable Ace: False), Dealer Score: 4\n",
      "Taking action: Hit\n",
      "Player Score: 16 (Usable Ace: False), Dealer Score: 4\n",
      "Taking action: Hit\n",
      "Player Score: 24 (Usable Ace: False), Dealer Score: 4\n",
      "Game end. Reward: -1.0\n",
      "\n",
      "Player Score: 20 (Usable Ace: False), Dealer Score: 7\n",
      "Taking action: Stick\n",
      "Player Score: 20 (Usable Ace: False), Dealer Score: 7\n",
      "Game end. Reward: 1.0\n",
      "\n",
      "Player Score: 15 (Usable Ace: False), Dealer Score: 7\n",
      "Taking action: Hit\n",
      "Player Score: 21 (Usable Ace: False), Dealer Score: 7\n",
      "Taking action: Stick\n",
      "Player Score: 21 (Usable Ace: False), Dealer Score: 7\n",
      "Game end. Reward: 1.0\n",
      "\n",
      "Player Score: 15 (Usable Ace: False), Dealer Score: 8\n",
      "Taking action: Hit\n",
      "Player Score: 23 (Usable Ace: False), Dealer Score: 8\n",
      "Game end. Reward: -1.0\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def print_observation(observation):\n",
    "    score, dealer_score, usable_ace = observation\n",
    "    print(\"Player Score: {} (Usable Ace: {}), Dealer Score: {}\".format(\n",
    "          score, usable_ace, dealer_score))\n",
    "\n",
    "def strategy(observation):\n",
    "    score, dealer_score, usable_ace = observation\n",
    "    # Stick (action 0) if the score is > 20, hit (action 1) otherwise\n",
    "    return 0 if score >= 20 else 1\n",
    "\n",
    "for i_episode in range(20):\n",
    "    observation = env.reset()\n",
    "    for t in range(100):\n",
    "        print_observation(observation)\n",
    "        action = strategy(observation)\n",
    "        print(\"Taking action: {}\".format( [\"Stick\", \"Hit\"][action]))\n",
    "        observation, reward, done, _ = env.step(action)\n",
    "        if done:\n",
    "            print_observation(observation)\n",
    "            print(\"Game end. Reward: {}\\n\".format(float(reward)))\n",
    "            break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
